﻿Imports System.Data.OleDb
Public Class Database_
    Private _connectionstring As String
    Private connection As OleDbConnection
    Public Sub New()
        _connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\Data\QuanLyNHanSuCongTy.mdb"
    End Sub
    Public Sub openconnection()
        Try
            If connection Is Nothing Then
                connection = New OleDbConnection(_connectionstring)
            End If
            connection.Open()
        Catch ex As Exception

        End Try
    End Sub
    Public Sub closeconnection()
        If connection Is Nothing Then
        Else
            connection.Close()
        End If
    End Sub
    Public Function execQuery(ByVal sql As String) As DataTable
        openconnection()
        Dim dt As New DataTable
        Dim command As OleDbCommand = connection.CreateCommand
        command.CommandText = sql
        Dim adapter As New OleDbDataAdapter
        adapter.SelectCommand = command
        adapter.Fill(dt)
        closeconnection()
        Return dt
    End Function

    Public Function ExecNonQuery(ByVal sql As String) As Boolean
        openconnection()
        Dim command As OleDbCommand = connection.CreateCommand
        command.CommandText = sql
        Dim kq As Integer = command.ExecuteNonQuery
        closeconnection()
        If kq > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function ExecUpdate(ByVal sql As String, ByVal dt As DataTable) As Boolean
        openconnection()
        Dim adapter As New OleDbDataAdapter(sql, connection)
        Dim cmdBuilder As New OleDbCommandBuilder(adapter)
        Dim kq As Integer = adapter.Update(dt)
        closeconnection()
        If kq > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
